package vip.xiaonuo.smzq.modular.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.user.entity.Org;
import vip.xiaonuo.smzq.modular.user.entity.Role;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.entity.vo.UserVo;
import vip.xiaonuo.smzq.modular.user.mapper.OrgMapper;
import vip.xiaonuo.smzq.modular.user.mapper.RoleMapper;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;
import vip.xiaonuo.smzq.modular.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户 服务实现类
 *
 * @author sqq
 * @since 2024-04-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;//角色
    @Resource
    private OrgMapper orgMapper;//机构


    @Override
    public Page<UserVo> page(long current, long size, String account, String name) {
        Page<User> page = new Page<>(current, size);
        Page<UserVo> pageVo = new Page<>();
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User userNow = userMapper.selectById(saBaseLoginUser.getId());
        Role position = roleMapper.selectById(userNow.getPositionId());
        String code = position.getCode();
        if (code.equals("shengsh")) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            String orgid = userNow.getOrgId().replaceAll("0+$", "");

            queryWrapper.like("org_id", orgid).eq("user_status", "DISABLED").eq("delete_flag", "NOT_DELETE");
            queryWrapper.or().eq("org_id","").eq("user_status", "DISABLED").eq("delete_flag", "NOT_DELETE");
            queryWrapper.or().isNull("org_id").eq("user_status", "DISABLED").eq("delete_flag", "NOT_DELETE");

            if (account != null && account.length() > 0) {
                queryWrapper.like("account", account);
            }
            if (name != null && name.length() > 0) {
                queryWrapper.like("name", name);
            }
            queryWrapper.orderByDesc("create_time");
            page = userMapper.selectPage(page, queryWrapper);
            List<User> userList = page.getRecords();
            List<UserVo> userVoList = new ArrayList<>();
            for (User user : userList) {
                UserVo UserVo = new UserVo();
                BeanUtil.copyProperties(user, UserVo);
                if (user.getOrgId() != null && user.getOrgId().length() > 0) {
                    Org org = orgMapper.selectById(user.getOrgId());
                    UserVo.setOrgName(org.getName());
                }
                if (user.getPositionId() != null && user.getPositionId().length() > 0) {
                    Role role = roleMapper.selectById(user.getPositionId());
                    UserVo.setRoleName(role.getName());
                }
                userVoList.add(UserVo);
            }
            pageVo.setRecords(userVoList);
            pageVo.setTotal(page.getTotal());
            pageVo.setCurrent(page.getCurrent());
            pageVo.setSize(page.getSize());
            return pageVo;
        }
        return pageVo;
    }
}
